<?php

namespace app\mobileapp\controller;

use think\Controller;
use think\facade\Cache;
use think\Db;

class Base extends Controller
{
    protected $token = '';
    protected $platform = 'web';

    private $_support_set_get_field = [
        'memberId', 'member'
    ];

    // 初始化函数
    public function initialize()
    {
        header('Content-type: application/json;chartset=UTF-8');
        header('Access-Control-Allow-Headers:X-Requested-With, Content-Type, accept-language, tt, platform');
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS');
        $this->token = $this->request->header('tt');
        $this->platform = $this->request->header('platform');
        $this->platform = 'unified';
    }

    /**
     * User: Simony
     * Function: set拦截器
     * @param $property_name 变量名
     * @param $value 值
     * @return mixed
     */
    public function __set($property_name, $value)
    {
        if (!in_array($property_name, $this->_support_set_get_field)) $this->api_ajaxReturn(ERROR, '非类属性');
        return $this->$property_name = $value;
    }

    /**
     * User: Simony
     * Function: get拦截器
     * @param $property_name 变量
     * @return mixed
     */
    public function __get($property_name)
    {
        if (!in_array($property_name, $this->_support_set_get_field)) $this->api_ajaxReturn(ERROR, '类属性不存在');

        $res = $this->getMemberData();
        $this->memberId = $res['id'];
        $this->member = $res;
        return $property_name == 'memberId' ? $res['id'] : $res;
    }


    /**
     * User:Simony
     * Function:
     * @param $code
     * @param $msg
     * @param $data
     */
    public function api_ajaxReturn($code, $msg = '', $data = [])
    {
        $data = json_encode(['code' => $code, 'data' => $data, 'msg' => $msg]);
        if ($code == SUCCESS && $callback = input('get.callback', false)) {
            echo "{$callback} ({$data})";
        } else {
            echo $data;
        }
        exit;
    }


    /**
     * User: Simony
     * Function:新方法
     * @return mixed|
     */
    private function getMemberData()
    {
        (empty($this->platform) || empty($this->token)) && $this->api_ajaxReturn(JUMP_LOGIN, '登录凭证不能为空');

        $tokenName = $this->platform . '-' . $this->token;
        $data = json_decode(\cache($tokenName) ?: '[]', true);

        $tokenModel = model('common/member/Token');
        if (empty($data)) {
            $data = $tokenModel->getInfo(['token' => $this->token, 'platform' => $this->platform]);
            empty($dbData) && $this->api_ajaxReturn(JUMP_LOGIN, '登陆信息已过期，请重新登陆');
            \cache($tokenName, json_encode($data));
        }

        $res = json_decode($data['data'], true);
        (!isset($res['expire_time']) || $res['expire_time'] < time()) && $this->api_ajaxReturn(JUMP_LOGIN, '登陆信息已过期，请重新登陆');
        $current_time = time();

        if ($res['generation_time'] < $current_time) {
            $res['generation_time'] = $current_time + model('common/member/TokenLogic', 'logic')->generation_time; //外面的操作时间
            $data['data'] = json_encode($res);  //data里面更新
            $tokenModel->edit(['member_id' => $res['id'], 'platform' => $this->platform], ['generation_time' => $res['generation_time'], 'data' => $data['data']]);
            \cache($tokenName, json_encode($data));
        }

        //自动升级
        model('common/member/LevelLogic', 'logic')->upgrade($res['id']);
        return $res;
    }
}